define(function(require, exports){
	var pubjs	= require('pubjs');
	var util	= require('util');
	var $		= require('jquery');
	var grid	= require('@base/highGrid');
	var tip		= require('@base/tip');
	var common	= require('modules/common');
	
	// 创意包审核
	var SweetyVerify = grid.base.extend({
		init: function(config){
			config = pubjs.conf(config, {
				'url': 'sweety/listVerify',
				'cols':[
					{type: 'id'},
					{name:"_id", text:'ID', sort: true, width: 60, align: 'center'},
					'SweetyNameWithThumb'
				],
				'hasTab': false,
				'hasAmount': false,
				'hasExport': false,
				'hasSwitch': false,
				'hasSubGrid':false,
				'hasMenu': true,
				'reqType': 'websocket',
				'param': {
					'order':'CreateTime|-1'
				}
			});

			this.Super('init', arguments);
		},
		renderName: function(i, val, data, con){
			var link = pubjs.config('materialPreview/sweety');
			var url = link.replace('{id}', data._id);
			var dom = $('<a target="_blank" href="'+url+'" date-index="'+i+'"/>');

			dom.addClass('M-grid-thumb');
			if (val === ''){
				dom.html(LANG('<i class="tdef">(空)</i>'));
			}else {
				$('<span class="fl">').text(val).appendTo(dom);
			}

			// 缩略图
			var h = 50, w = 50;
			var thumb = data.Cover || null;
			var icon  = thumb ? common.imageThumb(thumb, w, h) : null;
			icon = common.loadImage('sweety', icon).prependTo(dom);

			// @todo front_base还没有配置
			// var http_proto  = /^(http|https):\/\//i;
			// if (thumb && !http_proto.test(thumb)){
			// 	thumb = pubjs.config('front_base') + thumb;
			// }

			icon.attr({'width':w, 'height':h, 'data-origin':thumb});
			icon.css({'width': w, 'height': h});
			icon.addClass('mr5 fl');
			dom.find('span').css({'line-height': h+'px'});
			this.uiBind(icon, 'mouseenter mouseleave imageLoad imageError', 'showPreviewImage');
			return dom;
		},
		showPreviewImage: function(evt,el){
			var self = this;
			var tip  = self.get('previewImage');
			el = $(el);
			switch (evt.type){
				case 'mouseenter':
					//链接地址
					if (el.attr('data-ready') !== '1'){ break; }
					//图片地址
					var href = el.attr('data-origin');
					var img = common.imageThumb(href, 200, 200);
					var html = '<a href='+ href +' target="_blank"><img width="200" src='+ img +'></a>';
					var tipBody;
					//弹出框
					if (!tip){
						tip = self.createAsync('previewImage', '@base/tip.base', {
							'pos':'bm',
							'anchor': el,
							'delayHide': 500,
							"autoShow": true,
							"drag": false
						}, function(mod){
							tipBody = mod.getContainer();
							tipBody.html(html);
						});
					}else {
						tipBody = tip.getContainer();
						tipBody.html(html);
						tip.show();
						tip.update({anchor: el});
					}
				break;
				case 'mouseleave':
					if (tip){ tip.hide(); }
				break;
				case 'imageLoad':
					el.attr('data-ready', '1');
				break;
				case 'imageError':
					el.attr('data-thumb', null);
				break;
			}
		}
	});
	exports.sweety = SweetyVerify;


	// 创意审核
	var CreativeVerify = grid.base.extend({
		init: function(config){
			config = pubjs.conf(config, {
				'hasTab': false,
				'hasAmount': false,
				'hasExport': false,
				'hasSwitch': false,
				'hasSubGrid':false,
				'url': 'creative/listVerify',
				'urlReAudit': 'creative/reAudit',
				'param': {
					'order':'CreateTime|-1'
				},
				"cols": [
					{type: 'id'},
					{name:'_id',text:LANG("ID"), width: 60, align: 'center'},
					'CreativeNameWithThumb'
				],
				'reqType': 'websocket',
				'hasFilter': false,
				'gridName': 'creativeVerify'
			});
			this.Super('init', arguments);
		},
		renderStatus: function(index, val, row, col){
			// 因为列表锁的原因，每一列的data-name都不能重复，原本都是重复的"VerifyResult"作为data-name
			// 所以在状态渲染这里，用row.VerifyResult
			var VerifyResult = row.VerifyResult;
			var v = util.find(VerifyResult, col.param, "AdxId");
			if(v)
			{
				var html = '';
				switch(v.Status){
					// 通过
					case 'PASS' :
						// html = '<span class="P-verifyStatus"><div title="'+LANG("通过")+'" class="status"><span class="icon done" /><span class="text">'+LANG("通过")+'</span></div></span>';
						html = '<span class="P-verifyStatus"><div title="'+LANG("通过")+'"><span class="icon done" /></div></span>';
						break;
					// 部分通过	
					case 'PARTLY_PASS' :
						html = '<div class="P-verifyStatus"><div title="'+LANG("部分通过")+'" class="status"><span class="icon ing" /><span class="text">'+LANG("部分通过")+'</span></div></div>';
						break;
					// 待审核
					case 'WAITING' :
						html = '<div class="P-verifyStatus"><div title="'+LANG("待审核")+'"><span class="icon wait" /></div></div>';
						break;
					// 审核中
					case 'PROCESSING' :
						html = '<div class="P-verifyStatus"><div title="'+LANG("审核中")+'"><span class="icon ing" /></div></div>';
						break;
					// 拒绝
					case 'REFUSE' :
						html = '<div class="P-verifyStatus"><div title="'+LANG("未通过")+'"';
						// html += v.RejectReason.indexOf('系统判断：') === 0 ? 'sys' : 'err';
						html +='><span class="icon reject"  " data-reason="'+util.html(v.RejectReason)+'"/><span  class="text">'+LANG("未通过")+'</span></div>';
						//todo 审核信息
						// html += '<p class="text_limit rejectReason tl" title="'+util.html(v.RejectReason)+'">'+util.html(v.RejectReason)+'</p></div>';
					// 拒绝原因
						html = $(html);
						this.uiBind(html.find('.reject'), 'mouseenter mouseleave', this.eventRejectReason);
						break;
				}
				return html;

			}else{
				return null;
			}
		},
		renderHeader: function(i, val, data, con){
			var dom = $([
				'<div>',
					'<div>'+val+'</div>',
					'<input data-id="'+data.param+'" type="button" value='+LANG("重新审核")+' class="btn btn-confirm"/>',
				'</div>'
			].join(''));

			this.uiBind(dom.find('input'), 'click', 'eventReverify');

			return dom;
		},
		// 重新审核
		eventReverify: function(ev, el)
		{
			var self = this;
			var url = self.getConfig('urlReAudit');
			var AdxId = $(el).attr('data-id');
			var gridParam = this.getParam();
			
			if (!+AdxId || !gridParam || !+gridParam.QualificationId)
			{
				return false;
			}
			
			pubjs.mc.send(url,{
				'AdxId': +AdxId,
				'SweetyId': +self.$customParam.SweetyId,
				'QualificationId': +gridParam.QualificationId
			},function(err, data){
				if(err){
					pubjs.alert(err.message);
					return false;
				}
				self.reload();
			});
		},
		//审核拒绝原因
		eventRejectReason: function(evt, elm){
			var rejectTip  = this.get('rejectReasonTip');
			switch (evt.type){
				case 'mouseenter':
					elm = $(elm);
					var rejectReason = elm.attr("data-reason");

					if (!rejectTip){
						rejectTip = this.create('rejectReasonTip',tip.base, {
							'autoHide': 'leave',
							'width': 240,
							'pos': 'ml',
							'anchor': elm,
							// 'autoHide': 'click_body',
							'autoShow': false
						});
						rejectTip.setData(rejectReason);
						rejectTip.show();
					}else{
						rejectTip.update({anchor: elm});
						rejectTip.setData(rejectReason);
						rejectTip.show();
					}
				break;
			}
			return false;
		}

	});
	exports.creative= CreativeVerify;

});